package top.kuanghua.gateway.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @Title: testController
 * @Description:
 * @Auther:
 * @create 2020/8/18 22:09
 */
@DefaultProperties(defaultFallback = "fallBackMethod") // 指定一个类的全局熔断方法
public class testController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    @ResponseBody
    @HystrixCommand() // 标记该方法需要熔断
    //@HystrixCommand(fallbackMethod = "queryUserByIdFallBack") // 标记该方法需要熔断
    public String queryUserById(@RequestParam("id") Long id) {
        String user = this.restTemplate.getForObject("http://service-provider/user/" + id, String.class);
        return user;
    }

    /**
     * 熔断方法
     * 返回值要和被熔断的方法的返回值一致
     * 熔断方法不需要参数
     * @return
     */
    public String fallBackMethod(){
        return "请求繁忙，请稍后再试！";
    }
    //相同的参数列表和返回值声明
    public String queryUserByIdFallBack(Long id){
        return "请求繁忙，请稍后再试！";
    }
}
